package com.cn.haspital.config.token_auth;

import cn.dev33.satoken.stp.StpInterface;
import com.cn.hospital.service.system.SysPermissionService;
import com.cn.hospital.service.system.SysRoleService;
import lombok.extern.log4j.Log4j2;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Log4j2
public class SaTokenAuthStpImpl implements StpInterface {

    @Reference
    SysRoleService sysRoleService;
    @Reference
    SysPermissionService sysPermissionService;

    /**
     * 权限码集合查询
     *
     * @param loginId
     * @param loginType
     * @return
     */
    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        List<String> permissionList = sysPermissionService.findUserPermissionById(Long.valueOf((String) loginId));
        log.info("Sa-Token token_auth permission : {}", permissionList);
        return permissionList;
    }


    /**
     * 角色查询
     *
     * @param loginId
     * @param loginType
     * @return
     */
    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        List<String> roleList = sysRoleService.findUserRoleById(Long.valueOf((String) loginId));
        log.info("Sa-Token token_auth role : {}", roleList);
        return roleList;
    }

    /**
     * 集合判断是否为null
     * @param list
     * @return
     */
    public static Boolean isNotNullList(List<String> list) {
        if (list != null || list.size() >= 1) {
            return true;
        }

        return false;
    }

    /**
     * 判断权限是否有 *
     * @param list
     * @return
     */
    public static Boolean isRolePermissionAll(List<String> list) {
        for (String isAll : list) {
            if ("*".equals(isAll)) {
                return true;
            }
        }
        return false;
    }
}

